package net.wenku8.reader.view.user

import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import net.wenku8.reader.LocalNavController
import net.wenku8.reader.R
import net.wenku8.reader.cache.AvatarCacheHandler
import net.wenku8.reader.enums.RouteAccountDetail
import net.wenku8.reader.enums.RouteAppDetail
import net.wenku8.reader.enums.RouteCacheClear
import net.wenku8.reader.enums.RouteLogin
import net.wenku8.reader.state.StateLogin
import net.wenku8.reader.ui.lite.LiteBlank
import net.wenku8.reader.ui.lite.LiteButton
import net.wenku8.reader.ui.lite.LiteCard
import net.wenku8.reader.ui.lite.LitePanel
import net.wenku8.reader.ui.lite.LiteText
import net.wenku8.reader.ui.lite.LiteTitle
import net.wenku8.reader.ui.lite.cardImage
import net.wenku8.reader.view.common.CacheImage
import net.wenku8.reader.view.common.ConfirmLayer
import net.wenku8.reader.view.common.ConfirmViewModel
import androidx.compose.runtime.collectAsState


@Composable
fun User() {
    val userViewModel = viewModel<UserViewModel>()
    val confirmViewModel = viewModel<ConfirmViewModel>()
    val navController = LocalNavController.current
    LaunchedEffect(Unit) {
        userViewModel.initData()
    }
    LitePanel(
        hostState = userViewModel.hostState,
        title = "用户信息"
    ) {
        Column{
            if (StateLogin.status.collectAsState().value){
                LiteCard(
                    left = {
                        val cacheHandler = remember { AvatarCacheHandler() }
                        CacheImage(cacheHandler, modifier = Modifier.cardImage(), defaultShowImage = true, defaultImage = R.mipmap.noavatar)
                    },
                    header = {
                        LiteTitle(userViewModel.userInfo.uname)
                    },
                ){
                    Column {
                        LiteText(text = "昵称："+userViewModel.userInfo.nickname)
                        LiteText(text = "等级："+userViewModel.userInfo.rank)
                    }
                }
                LiteBlank()
                LiteButton(text = "账户详情") {
                    navController.navigate(RouteAccountDetail)
                }
                LiteButton(text = "文库签到") {
                    userViewModel.userSign()
                }
                LiteButton(text = "退出登录") {
                    confirmViewModel.show("你确定要退出登陆吗？"){
                        userViewModel.onLogout()
                    }
                }
            }else{
                LiteButton(text = "登录文库") {
                    navController.navigate(RouteLogin)
                }
            }
            LiteBlank()
            LiteButton(text = "清除缓存") {
                navController.navigate(RouteCacheClear)
            }
            LiteButton(text = "应用详情") {
                navController.navigate(RouteAppDetail)
            }
        }
        ConfirmLayer()
    }

}

